package com.fencing_squad.homo.list;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;

public class Post {
    private String nickname = "";
    private String content = "";
    private int likes = 0;
    private int comment = 0;
    private String avatar = "";
    private String id = "";
    private String userId = "";
    private long timestamp = 0;

    public Post() {}

    public Post(JSONObject object) {
        JSONObject result = object.getJSONObject("result");
        comment = result.getIntValue("comment_num");
        likes = result.getIntValue("like_num");
        id = result.getString("post_message_id");
        userId = result.getString("user_id");
        avatar = object.getString("avatar");
        content = result.getString("content");
        nickname = object.getString("nick_name");
        timestamp = result.getLong("post_time");
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getLikes() {
        return String.valueOf(likes)+"点赞";
    }

    public void setLikes(int likes) {
        this.likes = likes;
    }

    public String getComment() {
        return String.valueOf(comment)+"评论";
    }

    public void setComment(int comment) {
        this.comment = comment;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public static ArrayList<Post> getFromJSONArray(JSONArray array) {
        if (array == null) {
            return null;
        }
        ArrayList<Post> posts = new ArrayList<>();
        for(int i=0; i<array.size(); i++) {
            posts.add(new Post(array.getJSONObject(i)));
        }
        return posts;
    }
}
